/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#include <core/domain.h>

#if CONFIG_DOM_NUMBER > 1
const dschedule_t domSchedule[CONFIG_DOM_NUMBER] = {
    { .domainId = 0, .timeLength = 20 * 1000000 },
    { .domainId = 1, .timeLength = 20 * 1000000 },
};
#else
const dschedule_t domSchedule[CONFIG_DOM_NUMBER] = {
    { .domainId = 0, .timeLength = 10000 },
};
#endif

dom_t        domScheduleLength;
dom_t        curDomainId;
unsigned int curDomainTime;

void domain_init(unsigned int dom_id)
{
#if CONFIG_DOM_NUMBER <= 1
    domScheduleLength = 1;
    curDomainId       = 0;
    curDomainTime     = 10000;
#else
    domScheduleLength = sizeof(domSchedule) / sizeof(dschedule_t);
    if (dom_id >= domScheduleLength) {
        dom_id = 0;
    }
    curDomainId   = dom_id;
    curDomainTime = domSchedule[curDomainId].timeLength;
#endif
}

void deductCurDomainTime(int offset)
{
    if (curDomainTime <= offset) {
        curDomainTime = 0;
    } else {
        curDomainTime -= offset;
    }
}

unsigned int getCurDomainId(void)
{
    return curDomainId;
}

unsigned int getCurDomainTime(void)
{
    return curDomainTime;
}
